{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a36ace17",
   "metadata": {},
   "source": [
    "-----\n",
    "* 记录人（学习人）：卢颖怡\n",
    "* 时间：2021-11-29\n",
    "* week14\n",
    "* 主要参考文档：\n",
    "[python官方文档](https://docs.python.org/doc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d720bc",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. 函数的定义\n",
    "> 2. 函数的使用\n",
    "> 3. 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "570c07ec",
   "metadata": {},
   "source": [
    "# 函数的定义\n",
    "> 定义一个函数的一般步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ff0dc5da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a, end=' ') # 0 1 1\n",
    "    a, b = b, a+b # a = 1 a =1 b =2 a = 2 b = 3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "02c9e0b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 100:\n",
    "    print(a, end=' ') # 0 1 1\n",
    "    a, b = b, a+b # a = 1 a =1 b =2 a = 2 b = 3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffad0662",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ..... 我想重复使用某一个功能（避免重复造轮子）\n",
    "# ..... 使用的人可以不懂代码，只要会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "414c3954",
   "metadata": {},
   "source": [
    "> 1. 定义函数的一般方法：\n",
    "...\n",
    "\n",
    "def function_name(): # 1.function_name 遵循变量定义的规则 区别数学函数==> f(x) python的函数可以自定义名称\n",
    "    \"\"\"函数的注释\"\"\" # 2.函数的注释可以和使用者交流\n",
    "    函数体          # 3. 函数体要和定义函数保持一定的tab缩进\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c78dac1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b54288b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6d1c060d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 \n"
     ]
    }
   ],
   "source": [
    "fib(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ad0641b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 \n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c551b51d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 请大家写一个函数 实现 hello world\n",
    "# 2. 请大家写一个函数 实现 hello username ~\n",
    "\n",
    "def hello():    \n",
    "    \"\"\"打印你好，世界\"\"\"    \n",
    "    print(\"hello world\")         \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2d93b6d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "40ec6b9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello_user(username):\n",
    "    \"\"\"打印,你好，xxx同学~\"\"\" \n",
    "    print(\"你好，\"+ username.title()+\"同学~\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "be5806c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，Jack同学~\n"
     ]
    }
   ],
   "source": [
    "hello_user(\"jack\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2e562ef",
   "metadata": {},
   "source": [
    "* 思考 ：函数是否必须有 参数？\n",
    "* 注意 ：\n",
    ">      （1）在第一行定义函数中，不需要任何信息就能完成工作，因此括号是空的（即便如此，括号必不可少！）\n",
    ">　　　 （2）要调用函数，可以依次指定函数名以及括号括起的必要信息。在第五行代码中，因为此函数greet_uesr()括号中不需要任何信息，只需要输入greet_uesr（）即可。和预期效果一样，打印Hello！：\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e8f810a",
   "metadata": {},
   "source": [
    "## 3.函数的参数\n",
    "\n",
    "> 1. 形式参数：\n",
    ">> 1. 定义函数时，写在function_name 括号里面的参数\n",
    ">> 2. 在定义函数时，形式参数不占用空间\n",
    "> 2. 实际参数：\n",
    ">> 1. 调用参数时，写在function_name 括号里面的参数\n",
    ">> 2. 在使用函数时，形式参数 被 分配空间，变为实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f21fe4d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):  # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5e1e74a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cc5181a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a 豆豆.\n",
      "My 豆豆's name is Dog.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"dog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf9993e4",
   "metadata": {},
   "source": [
    "* 当函数具有多个参数时\n",
    "\n",
    "> 1. 位置参数\n",
    ">> 1. 形式参数有先后顺序，函数调用过程中应考虑参数的先后顺序，避免出现顺序错误的笑话\n",
    ">> 2. 调用函数过程中要考虑形式参数的个数\n",
    "> 2. 关键字参数\n",
    ">> 1. 调用函数时，通过赋值的方式传递给定义好的形参变量数据\n",
    ">> 2. 关键字参数不考虑先后顺序，因为是指定变量赋值\n",
    "> 3. 默认参数\n",
    ">> 1. 在形式参数中，提前预先设置一个用户可能使用的参数值\n",
    ">> 2. 用户在使用/调用函数时，可以省略不给默认参数赋值\n",
    ">> 3. 用户给默认参数赋值，将替换默认参数的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9d0c535c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):  # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"显示宠物信息    \"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "40f7b6a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 在使用 describe_pet() 函数时 需要考虑 参数的位置， 从而进行参数数据的输入 （实参的调用）\n",
    "# 位置参数：\n",
    "describe_pet(\"dog\",\"豆豆\") # 要根据 形式参数的位置先后顺序  先写animal_type,再写pet_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "faeafbf6",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 2 required positional arguments: 'animal_type' and 'pet_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-f95e5f55e3d1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 2 required positional arguments: 'animal_type' and 'pet_name'"
     ]
    }
   ],
   "source": [
    "describe_pet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8eb9864a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数使用\n",
    "\n",
    "describe_pet(pet_name=\"豆豆\",animal_type=\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "81fbe9e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数\n",
    "def describe_pet(pet_name,animal_type=\"dog\"):  # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"\n",
    "    显示宠物信息\n",
    "    \n",
    "    通过打印的方式显示宠物类型和宠物名称\n",
    "    参数信息：\n",
    "    pet_name： 是一个字符串，宠物名称\n",
    "    animal_type：是一个字符串，宠物类型\n",
    "    \"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "144784ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "34055c3a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "My cat's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"cat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "214fe159",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "#　help(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c73435b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello \n",
      " hello\n"
     ]
    }
   ],
   "source": [
    "print('hello','\\n','hello')\n",
    "# print('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e629f7d9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息\n",
      "    \n",
      "    通过打印的方式显示宠物类型和宠物名称\n",
      "    参数信息：\n",
      "    pet_name： 是一个字符串，宠物名称\n",
      "    animal_type：是一个字符串，宠物类型\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "392a5198",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",'\\n',\"hello\",sep='')\n",
    "# print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cb803ff0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19a87449",
   "metadata": {},
   "source": [
    "-------\n",
    "* 参数过程中的思考与问题：\n",
    "\n",
    "> 1. 思考：关键字参数(调用函数） 和 位置参数（调用函数） 是否可以一起使用？（可以）\n",
    ">> 1. positional argument follows keyword argument(位置参数<font style=\"color:red\">均</font>要放在关键字参数前面）\n",
    ">> 2. test1() got multiple values for argument 'x'(映射， 不能出现 multiple values (多重值））\n",
    "> 2. 定义函数: 默认参数和非默认参数的先后关系\n",
    ">> 1. non-default argument follows default argument(非默认参数<font style=\"color:red\">均</font>要放在默认参数前面\n",
    "------\n",
    "* 以下为测试代码：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d80bf872",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例1：\n",
    "def test1(x,y,z):\n",
    "    \"\"\"打印x,y,z的值\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "21113465",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-38-fd583303ea9b>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-38-fd583303ea9b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    test1(x=2,3,z=4)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "test1(x=2,3,z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9242e07d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "test1(2,y=3,z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "58f5cb05",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-40-32b91fe9ca8b>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-40-32b91fe9ca8b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    test1(2,y=3,4)\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "test1(2,y=3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "5759f466",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "test1(2,z=4,y=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2cd3c153",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "test1() got multiple values for argument 'x'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-42-abf767fdec53>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtest1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: test1() got multiple values for argument 'x'"
     ]
    }
   ],
   "source": [
    "test1(2,x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6745f537",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例2：\n",
    "def test1(x,z,y=3):\n",
    "    \"\"\"打印x,y,z的值\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51df72d3",
   "metadata": {},
   "source": [
    "## 4.课本练习实践\n",
    "\n",
    "> 1. 请大家总结函数的优势\n",
    "> 2. 预习课本第四章函数与模块\n",
    ">> 1. 完成课本元音字母实践\n",
    ">> 2. 浏览模块，创建模块和调用模块，了解PEP 8 python 国际代码标准\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c37e3d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels():\n",
    "    vowels = set('aeiou')\n",
    "    word = input(\"Provide a word to search for vowels: \")\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cc801229",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels: syre\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "search4vowels()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4237bdfe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0)  # If an object evaluates to 0, it is always False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "70180fcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "732b03b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " bool('') # An empty string, an empty list, and an empty dictionary all evaluate to False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39adf775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " bool([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bc957262",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool({})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7b2aaf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(None) # Python’s “None” value is always False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "53987b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " bool(0.0000000000000000000000000000001) # It may be really small, but it is still not 0, so it’s True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cdcf2177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool([42, 43, 44]) # A nonempty built-in data structure is True."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a50b404",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " bool({'a': 42, 'b':42})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "33950b09",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Display any vowels found in a supplied word.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "55f66228",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"phraell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7cd76715",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found.\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return bool(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7b8fe898",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "70bbbf2a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('sky')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "66d3c048",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found  # Return the results as a data structure (a set)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "650627a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))  # Return the data without the use of the unnecessary “found” variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "553f6f56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'i'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"virgil\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5b8b9591",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"sky\")  # An empty set is represented as set() by the interpreter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f3f9ecaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# annotations 使用注释\n",
    "def search4vowels(word:str) -> set:\n",
    "    \"\"\"Return any vowels found in a supplied word.\"\"\" \n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "56747c58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word: str) -> set\n",
      "    Return any vowels found in a supplied word.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8795ba6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(phrase: str) -> set:\n",
    "    \"\"\"return any vowels found in a supplied phrase\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(phrase)) # Perform a set intersection on the set object made from “letters” with the set object made from “phrase”.\n",
    " \n",
    "def search4letters(phrase:str,letters:str)->set:\n",
    "    \"\"\"return a set of the 'letters' found in 'phrase'. \"\"\"\n",
    "    return set(letters).intersection(set(phrase)) # Send the results back to the calling code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "842ab58c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4letters in module __main__:\n",
      "\n",
      "search4letters(phrase: str, letters: str) -> set\n",
      "    return a set of the 'letters' found in 'phrase'.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4letters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "597ffb21",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "061ede68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(0, 255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0b29c05c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"判断输入文本出现的元音字母有哪些\"\"\"\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a567a919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'o', 'e'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"dhwofsmfwoe\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b8704f8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'o'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"djrnsyowbdk\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4b7ffe4",
   "metadata": {},
   "source": [
    "## 下周预告\n",
    "### 函数返回值\n",
    "### 模块介绍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dabc2c87",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
